package org.example.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import org.example.entity.Dept;
import org.example.entity.Emp;
import org.example.pojo.Result;
import org.example.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @Autowired
    private HttpSession session;

    @GetMapping("/list")
    public Result<List<Dept>> list(Integer page,Integer limit,String searchDeptName){
        PageInfo<Dept> pageInfo = deptService.listByPage(page, limit,searchDeptName);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    //新增员工时的下拉框所需数据
    @GetMapping("/listAll")
    public Result<Object> listAll(){
        List<Dept> deptList = deptService.list();
        return Result.success(deptList,deptList.size());
    }
    //逻辑删除，把删除标志修改为1
    @DeleteMapping("/delete")
    public Result<Object> delete(Dept dept){
        UpdateWrapper<Dept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dept_id",dept.getDeptId());
        Emp empSession = (Emp) session.getAttribute("empSession");
        updateWrapper.set("delete_flag","1");
        //设置修改人和修改时间
        updateWrapper.set("update_by",empSession.getEmpName());
        updateWrapper.set("update_time",new Date());
        boolean updateResult = deptService.update(updateWrapper);
        if(updateResult){
            return Result.success(null,0);
        }else {
            return Result.fail("删除失败");
        }
    }
    @PostMapping("/add")
    public Result<Object> add(Dept dept){

        //判断部门是否已存在，因为实际业务中不会存在两个名字相同的部门
        String deptName = dept.getDeptName();
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getDeptName, deptName);
        List<Dept> list = deptService.list(wrapper);
        if(list.size() > 0){
            return Result.fail("该部门已存在");
        }
        //如果部门不存在，就允许新增
        //设置创建人和创建时间
        Emp empSession = (Emp) session.getAttribute("empSession");
        dept.setCreateBy(empSession.getEmpName());
        dept.setCreateTime(new Date());
        //保存
        boolean saveResult = deptService.save(dept);
        if(saveResult){
            return Result.success(null,0);
        }else {
            return Result.fail("保存失败");
        }
    }

    @PostMapping("/update")
    public Result<Object> update(Dept dept){
        Emp empSession = (Emp) session.getAttribute("empSession");
        dept.setUpdateBy(empSession.getEmpName());
        dept.setUpdateTime(new Date());
        boolean updateResult = deptService.updateById(dept);
        if(updateResult){
            return Result.success(null,0);
        }else{
            return Result.fail("编辑失败");
        }
    }

}
